<?php

namespace addons\xykeep\service;

use app\api\model\xykeep\User as UserModel;
use app\common\model\Store as StoreModel;
use app\api\model\xykeep\store\Commission;
use addons\xykeep\exception\Exception;
use think\Db;

/**
 * 商家业务
 */
class Store
{

    public $store;    // 商家


    // 例如在商家中心的管理，我们就一般直接用user_id 传
    // 用户下单，我们只记录store_id，就用store_id
    public function __construct($user_id=null, $store_id=null)
    {
        if($user_id){
            $userInfo = UserModel::where('id', $user_id)->find();
            $this->store = StoreModel::where(['phone'=>$userInfo['mobile']])->find();
        }
        if($store_id){
            $this->store = StoreModel::where('id', $store_id)->find();
        }

    }

    // DEPRECATED 通过商家id反查出商家用户id
    public static function getStoreUserId($store_id)
    {
        $user_id = null;
        $phone = StoreModel::where('id', $store_id)->value('phone');
        if($phone){
            $user_id = UserModel::where('mobile', $phone)->value('id');
        }
        return $user_id;
    }

    /**
     * @name 变更余额
     * @param  float        $money      变更金额
     * @param  string       $type       变更类型
     * @param  int          $service_id    业务ID
     * @param  string       $islog       是否记录余额日志
     * @return boolean
     */
    public function commission($money, $type = '', $service_id = 0, $islog = true){

        // 判断金额
        if ($money == 0) {
            return;
        }
        if( !$this->store){
            return;
        }

        $before = $this->store->commission;
        $after = $this->store->commission;
        // 只有后台操作，余额才可以是负值
        if ($after < 0 && !in_array($type, ['sys'])) {
            new Exception('可用余额不足');
        }
        try {

            if ($type == 'apply_withdraw') {
                //申请提现，变更提现中佣金
                $this->store->withdrawing_commission = $this->store->withdrawing_commission - $money;
            } else if ($type == 'refuse_withdraw') {
                //拒绝提现，变更分销商佣金信息和提现中佣金
                $this->store->commission = Db::raw('commission + ' . $money);
                $this->store->withdrawing_commission = Db::raw('withdrawing_commission - ' . $money);
            } else if ($type == 'pay_withdraw') {
                //提现打款，变更分销商提现中佣金和已提现佣金
                $this->store->withdrawn_commission = Db::raw('withdrawn_commission + ' . $money);
                $this->store->withdrawing_commission = Db::raw('withdrawing_commission - ' . $money);
            } else if ($type == 'order') {
                //商家订单下单
                $this->store->total_commission = Db::raw('total_commission + ' . $money);
            }else if ($type == 'order_over') {
                //商家订单结算
                $after = bcadd($after, $money,2);
            }

            //变更佣金
            $this->store->commission = $after;
            $this->store->save();

            //商家余额变更记录
            if ($islog) {
                (new Commission)::create([
                        'store_id' => $this->store->id,
                        'type' => $type,
                        'money' => $money,
                        'before' => $before,
                        'after' => $after,
                        'service_id' => $service_id
                    ]
                );
            }

        } catch (\Exception $e) {
            new Exception('您提交的数据不正确');
        }
    }


    //获取商家状态
    public function getStoreInfo()
    {
        $response = [
            'status' => 'normal',
            'data'   => $this->store,
            'msg'    => '商家详情'
        ];
        return $response;
    }



}
